package cc.wfu.domain.auth.service;

import cc.wfu.domain.auth.model.entity.AuthCodeEntity;
import cc.wfu.domain.auth.repository.IAuthRepository;
import cc.wfu.types.enums.Constants;
import cc.wfu.types.common.EmailService;
import cc.wfu.types.exception.UserServiceException;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 用户权限校验 - 验证码等
 * @Date: 2024-05-16-11:26
 */
@Slf4j
public abstract class AuthAbstractService implements IAuthService {

    @Resource
    protected EmailService emailService;

    @Resource
    protected IAuthRepository authRepository;

    /**
     * 生成验证码
     * @param mail 邮箱
     */
    @Override
    public String generateCode(String mail) {
        String code = authRepository.generateCode(mail);
        sendEmailCode(mail, code);
        return code;
    }

    /**
     * 验证验证码
     * @param authCodeEntity 参数： mail,code
     */
    @Override
    public void checkCode(AuthCodeEntity authCodeEntity) {
        if (!authRepository.checkCode(authCodeEntity)) {
            throw new UserServiceException(Constants.ResponseError.ILLEGAL_CODE_ERROR.getInfo());
        }
    }

    /**
     * 校验【密码】是否正确
     * @param encodePassword 数据库加密密码
     * @param verifyPassword 待验证密码
     */
    @Override
    public void checkPassword(String encodePassword, String verifyPassword) {
        verifyPassword(encodePassword, verifyPassword);
    }

    /**
     * 校验【密码】是否正确
     * @param encodePassword 数据库加密密码
     * @param verifyPassword 待验证密码
     */
    protected abstract void verifyPassword(String encodePassword, String verifyPassword);

    /**
     * 发送验证码
     * @param mail 邮箱
     * @param code 验证码
     */
    protected abstract void sendEmailCode(String mail, String code);
}
